var sPanel = $("#treesortable");
var zTreeObj;
var cNode;
$(function() {
	var setting = {
		check : {
			enable : false
		},
		async : {
			enable : true,
			url : "admin/treenode/tree-node!listall",
			autoParam : [ "id=bean.id" ],
			otherParam : {}
		},
		callback : {
			onExpand : zTreeOnExpand
		}
	};
	var root = {
		name : "系统全部权限",
		open : true,
		nocheck : false,
		id : "root",
		isParent : true
	};
	var zNodes = [ root ];
	zTreeObj = $.fn.zTree.init($("#resTree"), setting, zNodes);
	var nodes = zTreeObj.getNodes();
	zTreeObj.expandNode(nodes[0],true,false,true,true);

	$("#saveSorted").bind(
			"click",
			function() {
				var sort = sPanel.sortable("toArray");
				var qs = "mapDefaultActionMethod=false";
				$.each(sort, function(i, id) {
					qs += "&beans[" + i + "].id=" + id + "&beans[" + i
							+ "].sort=" + i;
				});
				$.ajax({
					"type" : "post",
					"data" : qs,
					"url" : "admin/treenode/tree-node!sorted",
					"success" : function(data, jqxhr) {
						zTreeObj.reAsyncChildNodes(cNode, "refresh");
					}
				});
			}).button();
});
function zTreeOnExpand(event, treeId, treeNode) {
	var nodes = treeNode.children;
	cNode = treeNode;
	sPanel.empty();
	$
			.each(
					nodes,
					function(i, nd) {
						var li = $("<li id='"
								+ nd.id
								+ "' class='ui-state-default'><span class='ui-icon ui-icon-arrowthick-2-n-s'></span>"
								+ nd.name + "</li>");
						sPanel.append(li);
					});
	$("#treesortable").sortable();
	$("#treesortable").disableSelection();

}